<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #669fef">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/register.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">
            欢迎注册文件收集系统
          </div>
          <el-form-item label="用户名" prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入用户名"
                      v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入姓名"
                      v-model="user.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码"
                      v-model="user.confirmPass"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" prop="phone">
            <el-input prefix-icon="el-icon-phone" size="medium" placeholder="请输入电话号码"
                      v-model="user.phone"></el-input>
          </el-form-item>       

         <el-form-item label="学号" prop="number">
           <el-input prefix-icon="el-icon-phone" size="medium" placeholder="请输学号"
                     v-model="user.number"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="info" style="width: 100%" @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex; justify-content: center; margin-top: 20px">
            <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2; cursor: pointer"
                                                       @click="$router.push('/login')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length > 10) {
        callback(new Error('用户名长度不能超过10个字符'));
      } else {
        callback();
      }
    };

    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.user.password) {
        callback(new Error('两次输入的密码不一致'));
      } else if (value.length > 20) {
        callback(new Error('密码长度不能超过20个字符'));
      } else {
        callback();
      }
    };

    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'));
      } else {
        // 这里可以添加电话号码的格式验证逻辑
        callback();
      }
    };


    return {
      user: {
        username: '',
        password: '',
        name: '',
        confirmPass: '',
        phone: '',
        number:'',

      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: validateUsername, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    register() {
      this.$refs.registerRef.validate((valid) => {
        if (valid) {
          const registerData = {
            username: this.user.username,
            password: this.user.password,
            phone: this.user.phone,
            name: this.user.name,
            number: this.user.number,
          };

          this.$request.post('/register', registerData).then(res => {
            if (res.code === '200') {
              this.$message.success('注册成功');
              this.$router.push('/login');
            } else {
              this.$message.error(res.msg);
            }
          }).catch(error => {
            console.error("注册失败:", error);
            this.$message.error('注册失败');
          });
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>
</style>